package com.coship.vas.dapg.util;

import java.io.File;
import java.io.FileInputStream;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * 
 * 
 * File Name : ConfigUtil.java
 * 
 * @Description : 属性获取工具
 * @author 904304 张益民
 */
public class ConfigUtil
{
	public final static String CONFIG_FILE = "/sysconfig.properties";
	private static long lastModified = 0L;
	private static File configFile = null;

	private static Properties props = new Properties();

	static
	{
		loadProperty();
	}

	private static void loadProperty()
	{
		try
		{
			String path = ConfigUtil.class.getResource(CONFIG_FILE).getFile();
			if (System.getProperty("os.name").startsWith("Windows"))
			{
				path = path.substring(1);
			}
			path = URLDecoder.decode(path, "UTF-8");
			File f = new File(path);
			lastModified = f.lastModified();
			configFile = f;
			props.load(new FileInputStream(f));
		} catch (Exception e)
		{
			e.printStackTrace();
			System.exit(-1);
		}
	}

	@SuppressWarnings("unused")
	private static void checkUpdate()
	{
		if (configFile != null)
		{
			long m = configFile.lastModified();
			if (m != lastModified)
			{
				lastModified = m;
				try
				{
					Properties prop = new Properties();
					prop.load(new FileInputStream(configFile));
					props = prop;
				} catch (Exception e)
				{
				}
			}
		}
	}

	public static String getConfig(String name, String defaultValue)
	{
		// checkUpdate();
		String ret = props.getProperty(name, defaultValue);
		if (ret == null)
		{
			return defaultValue;
		} else
		{
			return ret.trim();
		}
	}

	public static String getConfig(String name)
	{
		return getConfig(name, null);
	}

	public static void main(String[] args) throws Exception
	{
		System.out.println(getConfig("serverId"));
	}

}
